<?php

namespace App\Console\Commands;

use App\Jobs\SyncPaperDataJob;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

class MigrateSingleTopicsJob extends Command
{
    protected $signature   = 'migrate:single-topics-job {sheet*}';
    protected $description = '迁移关联数据';

    public function __construct()
    {
        parent::__construct();
    }

    public function handle()
    {
        set_time_limit(0);

        $sheet = $this->argument('sheet');

        $hasPaperIds = DB::connection('test')->table('papers')
                         ->select('id')
                         ->pluck('id')
                         ->toArray();

        $paperIds = DB::connection('test')->table('import')
                      // ->where('sheet', $sheet)
                      ->where('status', '<>', 1)
                      ->whereNotIn('paper_id', $hasPaperIds)
                      ->pluck('paper_id')
                      ->toArray();
        // $paperIds = ['2c9a2ebc7230085101724f1aa6ca3aa9'];

        $chunkList = array_chunk($paperIds, 10);

        foreach ( $chunkList as $chunkPaperIds ) {
            SyncPaperDataJob::dispatch([$chunkPaperIds]);
        }

        $this->info('Migration completed successfully.');
    }


}
